//
// Created by leisurexi on 12/2/20.
// gethostbyname.c
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>

void error_handling(char *message) {
    fputs(message, stderr);
    fputc('\n', stderr);
    exit(1);
}

int main(int argc, char *argv[]) {
    int i;
    char message[1024];
    struct hostent *host;

    fputs("Insert domain name: ", stdout);
    fgets(message, sizeof(message), stdin);
    int len = strlen(message);
    message[len - 1] = 0;
    host = gethostbyname(message);
    if (!host)
        error_handling("gethost... error");

    printf("Official name: %s \n", host->h_name);

    for (i = 0; host->h_aliases[i]; i++)
        printf("Aliases %d: %s \n", i + 1, host->h_aliases[i]);

    printf("Address type: %s \n", (host->h_addrtype == AF_INET) ? "AF_INET" : "AF_INET6");

    for (i = 0; host->h_addr_list[i]; i++)
        printf("IP addr %d: %s \n", i + 1, inet_ntoa(*(struct in_addr *) host->h_addr_list[i]));

    return 0;
}